package com.android.assignment.taskreminder;

import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

public class TaskReminderListFragment extends ListFragment implements
		LoaderCallbacks<Cursor> {

	private SimpleCursorAdapter mAdapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		setEmptyText(getResources().getString(R.string.no_reminder));

		String[] from = new String[] { TaskReminderProvider.COLUMN_TITLE };

		int[] to = new int[] { R.id.text1 };

		mAdapter = new SimpleCursorAdapter(getActivity(),
				R.layout.reminder_row, null, from, to, 0);
		setListAdapter(mAdapter);
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);

		Intent i = new Intent(getActivity(), TaskReminderEditActivity.class);
		i.putExtra(TaskReminderProvider.COLUMN_ROWID, id);
		startActivity(i);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.list_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		/*
		 * case R.id.menu_delete: AdapterContextMenuInfo info =
		 * (AdapterContextMenuInfo) item .getMenuInfo();
		 * getActivity().getContentResolver().delete(
		 * ContentUris.withAppendedId(TaskReminderProvider.CONTENT_URI,
		 * info.id), null, null); return true;
		 */
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		setEmptyText(getResources().getString(R.string.no_reminder));
		registerForContextMenu(getListView());
		setHasOptionsMenu(true);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_insert:
			editReminder(0);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		// TODO Auto-generated method stub
		return new CursorLoader(getActivity(),
				TaskReminderProvider.CONTENT_URI, null, null, null, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		// TODO Auto-generated method stub
		mAdapter.swapCursor(arg1);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub
		mAdapter.swapCursor(null);
	}

	public void editReminder(long id) {
		Intent i = new Intent(getActivity(), TaskReminderEditActivity.class);
		i.putExtra(TaskReminderProvider.COLUMN_ROWID, id);
		startActivity(i);
	}

}
